package com.ouheng.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ouheng.common.domain.Tree;
import com.ouheng.common.utils.BuildTree;
import com.ouheng.shop.dao.ProductDao;
import com.ouheng.shop.dao.RushPurchaseDao;
import com.ouheng.shop.dao.RushPurchaseProductDao;
import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.ProductGroupDO;
import com.ouheng.shop.domain.ProductProductGroupDO;
import com.ouheng.shop.domain.RushPurchaseDO;
import com.ouheng.shop.domain.RushPurchaseProductDO;
import com.ouheng.shop.service.RushPurchaseService;



@Service
public class RushPurchaseServiceImpl implements RushPurchaseService {
	@Autowired
	private RushPurchaseDao rushPurchaseDao;
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private RushPurchaseProductDao rushPurchaseProductDao;
	
	@Override
	public RushPurchaseDO get(Long id){
		return rushPurchaseDao.get(id);
	}
	
	@Override
	public List<RushPurchaseDO> list(Map<String, Object> map){
		return rushPurchaseDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return rushPurchaseDao.count(map);
	}
	
	@Override
	public int save(RushPurchaseDO rushPurchase){
//		List<RushPurchaseDO> listRushPurchase = rushPurchaseDao.list(null);
//		if(listRushPurchase.isEmpty()){
//			rushPurchase.setStatus("2");
//		}else{
//			if("2".equals(rushPurchase.getStatus())){
//				Map<String, Object> map = new HashMap<>();
//				map.put("status", 2);
//				List<RushPurchaseDO> listRushPurchaseDo = rushPurchaseDao.list(map);
//				for (RushPurchaseDO rushPurchaseDO : listRushPurchaseDo) {
//					rushPurchaseDO.setStatus("1");
//					rushPurchaseDao.update(rushPurchaseDO);
//				}
//			}
//		}
		
		return rushPurchaseDao.save(rushPurchase);
	}
	
	@Override
	public int update(RushPurchaseDO rushPurchase){
//		if("2".equals(rushPurchase.getStatus())){
//			Map<String, Object> map = new HashMap<>();
//			map.put("status", 2);
//			List<RushPurchaseDO> listRushPurchase = rushPurchaseDao.list(map);
//			for (RushPurchaseDO rushPurchaseDO : listRushPurchase) {
//				rushPurchaseDO.setStatus("1");;
//				rushPurchaseDao.update(rushPurchaseDO);
//			}
//		}
		return rushPurchaseDao.update(rushPurchase);
	}
	
	@Override
	public int remove(Long id){
		return rushPurchaseDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return rushPurchaseDao.batchRemove(ids);
	}

	@Override
	public Tree<ProductDO> getTree(Long id) {
		List<ProductDO> ids = productDao.list(new HashMap<String, Object>(16));
		List<Long> ppids = rushPurchaseProductDao.listProductidByPid(id);
		List<Long> temp = ppids;
//		for (ProductDO regionDO : ids) {
//			if (temp.contains(regionDO.getId())) {
//				ppids.remove(regionDO.getId());
//			}
//		}
		
		List<Tree<ProductDO>> trees = new ArrayList<Tree<ProductDO>>();
		List<ProductDO> regionDOs = productDao.list(new HashMap<String, Object>(16));
		for (ProductDO regionDO : regionDOs) {
			Tree<ProductDO> tree = new Tree<ProductDO>();
			tree.setId(regionDO.getId().toString());
			tree.setParentId(null);
			tree.setText(regionDO.getName());
			Map<String, Object> state = new HashMap<>(16);
			Long menuId = regionDO.getId();
			if (ppids.contains(menuId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ProductDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	@Transactional
	public void saveRelevance(RushPurchaseDO rushPurchase) {
		rushPurchaseProductDao.removeByPid(rushPurchase.getId());
		List<Long> ppids = rushPurchase.getPpids();
		for (Long long1 : ppids) {
			RushPurchaseProductDO rushPurchaseProductDO=new RushPurchaseProductDO();
			rushPurchaseProductDO.setPid(rushPurchase.getId());
			rushPurchaseProductDO.setProductid(long1);
			rushPurchaseProductDao.save(rushPurchaseProductDO);
		}
	}

	@Override
	@Transactional
	public void saveRelevance(Long pid, Long[] ppids) {
		rushPurchaseProductDao.removeByPid(pid);
		if(pid!=null&&ppids.length>0){
			for (Long string : ppids) {
				RushPurchaseProductDO rushPurchaseProductDO = new RushPurchaseProductDO();
				rushPurchaseProductDO.setPid(pid);
				rushPurchaseProductDO.setProductid(string);
				rushPurchaseProductDao.save(rushPurchaseProductDO);
			}
		}
		
	}
	
}
